/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerRegisterPanel.java
 *
 * Created on 2010-4-7, 10:40:15
 */
package ticketmaster.presentation.customer;

import java.util.logging.Level;
import java.util.logging.Logger;
import ticketmaster.presentation.LoginView;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.security.cert.CertificateException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import ticketmaster.accounting.TaxManager;
import ticketmaster.businesslogic.OrderController;
import ticketmaster.businesslogic.RegisterController;
import ticketmaster.businessobject.LoginUser;
import ticketmaster.businessobject.PurchaseType;
import ticketmaster.common.OrderType;
import ticketmaster.common.PurchaseTypes;
import ticketmaster.dataobject.User;
import ticketmaster.util.StringHelper;
import ticketmaster.dao.DAOFactory;
import ticketmaster.dao.UserDAO;
import ticketmaster.common.UserRoles;
import ticketmaster.dao.PremiumCustomerDAO;
import ticketmaster.dataobject.Customer;
import ticketmaster.dataobject.Event;
import ticketmaster.dataobject.Order;
import ticketmaster.dataobject.PremiumCustomer;
import ticketmaster.email.Email;
import ticketmaster.email.EmailSender;
import ticketmaster.presentation.HomeView;
import ticketmaster.presentation.administrator.AdministratorHomeView;
import ticketmaster.presentation.customer.purchase.PaymentReviewView;
import ticketmaster.security.Encryption;
import ticketmaster.util.Validator;
import ticketmaster.util.DateTimeHelper;

/**
 *
 * @author Tingting
 */
public class CustomerRegisterPanel extends CustomerBasePanel
{

    static final double YEARLYFEE = 5.0;
    private UserDAO userDAO;

    /** Creates new form CustomerRegisterPanel */
    public CustomerRegisterPanel()
    {
        initComponents();
        initPasswordFields();
        setComponentsNotVisibility();
    }

    private void setComponentsNotVisibility()
    {
        this.lbPasswordError.setVisible(false);
        this.lbPasswordError1.setVisible(false);
        this.lbAddress1Error.setVisible(false);
        this.lbAddress2Error.setVisible(false);
        this.lbCityError.setVisible(false);
        this.lbEmail2Error.setVisible(false);
        this.lbEmailError.setVisible(false);
        this.lbPhone.setVisible(false);
        this.lbPostcode.setVisible(false);
        this.lbProvinceError.setVisible(false);
        this.lbUserName.setVisible(false);
        this.lbfirstnameError.setVisible(false);
        this.lblastnameError.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbUsername = new javax.swing.JLabel();
        lbUsername1 = new javax.swing.JLabel();
        lbUsername2 = new javax.swing.JLabel();
        lbUsername3 = new javax.swing.JLabel();
        lbUsername4 = new javax.swing.JLabel();
        lbUsername5 = new javax.swing.JLabel();
        lbUsername6 = new javax.swing.JLabel();
        lbUsername7 = new javax.swing.JLabel();
        lbUsername8 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lbUsername10 = new javax.swing.JLabel();
        lbUsername11 = new javax.swing.JLabel();
        lbUsername12 = new javax.swing.JLabel();
        jBAccept = new javax.swing.JButton();
        jBLogIn = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jBBack = new javax.swing.JButton();
        lbPasswordError = new javax.swing.JLabel();
        lbUsername9 = new javax.swing.JLabel();
        pfPassword = new javax.swing.JPasswordField();
        pfPassword2 = new javax.swing.JPasswordField();
        lbPasswordError1 = new javax.swing.JLabel();
        lbUserName = new javax.swing.JLabel();
        lbPhone = new javax.swing.JLabel();
        lbAddress1Error = new javax.swing.JLabel();
        lbEmail2Error = new javax.swing.JLabel();
        lbEmailError = new javax.swing.JLabel();
        lblastnameError = new javax.swing.JLabel();
        tfFirstName = new javax.swing.JTextField();
        tfLastName = new javax.swing.JTextField();
        tfEmail = new javax.swing.JTextField();
        tfEmail2 = new javax.swing.JTextField();
        tfAddress1 = new javax.swing.JTextField();
        tfAddress2 = new javax.swing.JTextField();
        tfCity = new javax.swing.JTextField();
        tfProvince = new javax.swing.JTextField();
        tfPostcode = new javax.swing.JTextField();
        tfPhone = new javax.swing.JTextField();
        tfUserName = new javax.swing.JTextField();
        lbPostcode = new javax.swing.JLabel();
        lbProvinceError = new javax.swing.JLabel();
        lbCityError = new javax.swing.JLabel();
        lbAddress2Error = new javax.swing.JLabel();
        lbfirstnameError = new javax.swing.JLabel();
        jCheckBoxPremuim = new javax.swing.JCheckBox();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(CustomerRegisterPanel.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lbUsername.setFont(resourceMap.getFont("lbUsername.font")); // NOI18N
        lbUsername.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername.setText(resourceMap.getString("lbUsername.text")); // NOI18N
        lbUsername.setName("lbUsername"); // NOI18N
        add(lbUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 80, -1, 30));

        lbUsername1.setFont(resourceMap.getFont("lbUsername1.font")); // NOI18N
        lbUsername1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername1.setText(resourceMap.getString("lbUsername1.text")); // NOI18N
        lbUsername1.setName("lbUsername1"); // NOI18N
        add(lbUsername1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 120, 110, 30));

        lbUsername2.setFont(resourceMap.getFont("lbUsername2.font")); // NOI18N
        lbUsername2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername2.setText(resourceMap.getString("lbUsername2.text")); // NOI18N
        lbUsername2.setName("lbUsername2"); // NOI18N
        add(lbUsername2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 160, 130, 30));

        lbUsername3.setFont(resourceMap.getFont("lbUsername3.font")); // NOI18N
        lbUsername3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername3.setText(resourceMap.getString("lbUsername3.text")); // NOI18N
        lbUsername3.setName("lbUsername3"); // NOI18N
        add(lbUsername3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 360, 90, 30));

        lbUsername4.setFont(resourceMap.getFont("lbUsername4.font")); // NOI18N
        lbUsername4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername4.setText(resourceMap.getString("lbUsername4.text")); // NOI18N
        lbUsername4.setName("lbUsername4"); // NOI18N
        add(lbUsername4, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 400, 100, 30));

        lbUsername5.setFont(resourceMap.getFont("lbUsername5.font")); // NOI18N
        lbUsername5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername5.setText(resourceMap.getString("lbUsername5.text")); // NOI18N
        lbUsername5.setName("lbUsername5"); // NOI18N
        add(lbUsername5, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 320, 60, 30));

        lbUsername6.setFont(resourceMap.getFont("lbUsername6.font")); // NOI18N
        lbUsername6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername6.setText(resourceMap.getString("lbUsername6.text")); // NOI18N
        lbUsername6.setName("lbUsername6"); // NOI18N
        add(lbUsername6, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 240, 130, 30));

        lbUsername7.setFont(resourceMap.getFont("lbUsername7.font")); // NOI18N
        lbUsername7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername7.setText(resourceMap.getString("lbUsername7.text")); // NOI18N
        lbUsername7.setName("lbUsername7"); // NOI18N
        add(lbUsername7, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 440, 60, 30));

        lbUsername8.setFont(resourceMap.getFont("lbUsername8.font")); // NOI18N
        lbUsername8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername8.setText(resourceMap.getString("lbUsername8.text")); // NOI18N
        lbUsername8.setName("lbUsername8"); // NOI18N
        add(lbUsername8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, 180, 30));

        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(65, 36, -1, -1));

        lbUsername10.setFont(resourceMap.getFont("lbUsername10.font")); // NOI18N
        lbUsername10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername10.setText(resourceMap.getString("lbUsername10.text")); // NOI18N
        lbUsername10.setName("lbUsername10"); // NOI18N
        add(lbUsername10, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 520, 140, 30));

        lbUsername11.setFont(resourceMap.getFont("lbUsername11.font")); // NOI18N
        lbUsername11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername11.setText(resourceMap.getString("lbUsername11.text")); // NOI18N
        lbUsername11.setName("lbUsername11"); // NOI18N
        add(lbUsername11, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 560, 150, 30));

        lbUsername12.setFont(resourceMap.getFont("lbUsername12.font")); // NOI18N
        lbUsername12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername12.setText(resourceMap.getString("lbUsername12.text")); // NOI18N
        lbUsername12.setName("lbUsername12"); // NOI18N
        add(lbUsername12, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 480, 110, 30));

        jBAccept.setBackground(resourceMap.getColor("jBLogIn.background")); // NOI18N
        jBAccept.setFont(resourceMap.getFont("jBAccept.font")); // NOI18N
        jBAccept.setForeground(resourceMap.getColor("jBAccept.foreground")); // NOI18N
        jBAccept.setText(resourceMap.getString("jBAccept.text")); // NOI18N
        jBAccept.setName("jBAccept"); // NOI18N
        jBAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAcceptActionPerformed(evt);
            }
        });
        add(jBAccept, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 640, 200, 50));

        jBLogIn.setBackground(resourceMap.getColor("jBLogIn.background")); // NOI18N
        jBLogIn.setFont(resourceMap.getFont("jBLogIn.font")); // NOI18N
        jBLogIn.setForeground(resourceMap.getColor("jBLogIn.foreground")); // NOI18N
        jBLogIn.setText(resourceMap.getString("jBLogIn.text")); // NOI18N
        jBLogIn.setName("jBLogIn"); // NOI18N
        jBLogIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBLogInActionPerformed(evt);
            }
        });
        add(jBLogIn, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 30, -1, -1));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 10, -1, -1));

        jBBack.setBackground(resourceMap.getColor("jBLogIn.background")); // NOI18N
        jBBack.setFont(resourceMap.getFont("jBBack.font")); // NOI18N
        jBBack.setForeground(resourceMap.getColor("jBBack.foreground")); // NOI18N
        jBBack.setText(resourceMap.getString("jBBack.text")); // NOI18N
        jBBack.setName("jBBack"); // NOI18N
        jBBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBackActionPerformed(evt);
            }
        });
        add(jBBack, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 640, 200, 50));

        lbPasswordError.setForeground(resourceMap.getColor("lbPasswordError.foreground")); // NOI18N
        lbPasswordError.setText(resourceMap.getString("lbPasswordError.text")); // NOI18N
        lbPasswordError.setName("lbPasswordError"); // NOI18N
        add(lbPasswordError, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 520, -1, 30));

        lbUsername9.setFont(resourceMap.getFont("lbUsername9.font")); // NOI18N
        lbUsername9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername9.setText(resourceMap.getString("lbUsername9.text")); // NOI18N
        lbUsername9.setName("lbUsername9"); // NOI18N
        add(lbUsername9, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 280, 120, 30));

        pfPassword.setText(resourceMap.getString("pfPassword.text")); // NOI18N
        pfPassword.setName("pfPassword"); // NOI18N
        add(pfPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 520, 300, 30));

        pfPassword2.setText(resourceMap.getString("pfPassword2.text")); // NOI18N
        pfPassword2.setName("pfPassword2"); // NOI18N
        add(pfPassword2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 560, 300, 30));

        lbPasswordError1.setForeground(resourceMap.getColor("lbPasswordError1.foreground")); // NOI18N
        lbPasswordError1.setText(resourceMap.getString("lbPasswordError1.text")); // NOI18N
        lbPasswordError1.setName("lbPasswordError1"); // NOI18N
        add(lbPasswordError1, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 560, -1, 30));

        lbUserName.setForeground(resourceMap.getColor("lbUserName.foreground")); // NOI18N
        lbUserName.setText(resourceMap.getString("lbUserName.text")); // NOI18N
        lbUserName.setName("lbUserName"); // NOI18N
        add(lbUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 480, -1, 30));

        lbPhone.setForeground(resourceMap.getColor("lbPhone.foreground")); // NOI18N
        lbPhone.setText(resourceMap.getString("lbPhone.text")); // NOI18N
        lbPhone.setName("lbPhone"); // NOI18N
        add(lbPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 440, -1, 30));

        lbAddress1Error.setForeground(resourceMap.getColor("lbAddress1Error.foreground")); // NOI18N
        lbAddress1Error.setText(resourceMap.getString("lbAddress1Error.text")); // NOI18N
        lbAddress1Error.setName("lbAddress1Error"); // NOI18N
        add(lbAddress1Error, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 240, -1, 30));

        lbEmail2Error.setForeground(resourceMap.getColor("lbEmail2Error.foreground")); // NOI18N
        lbEmail2Error.setText(resourceMap.getString("lbEmail2Error.text")); // NOI18N
        lbEmail2Error.setName("lbEmail2Error"); // NOI18N
        add(lbEmail2Error, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 200, -1, 30));

        lbEmailError.setForeground(resourceMap.getColor("lbEmailError.foreground")); // NOI18N
        lbEmailError.setText(resourceMap.getString("lbEmailError.text")); // NOI18N
        lbEmailError.setName("lbEmailError"); // NOI18N
        add(lbEmailError, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 160, -1, 30));

        lblastnameError.setForeground(resourceMap.getColor("lblastnameError.foreground")); // NOI18N
        lblastnameError.setText(resourceMap.getString("lblastnameError.text")); // NOI18N
        lblastnameError.setName("lblastnameError"); // NOI18N
        add(lblastnameError, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 120, -1, 30));

        tfFirstName.setName("tfFirstName"); // NOI18N
        add(tfFirstName, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 80, 300, 30));

        tfLastName.setName("tfLastName"); // NOI18N
        add(tfLastName, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 120, 300, 30));

        tfEmail.setName("tfEmail"); // NOI18N
        add(tfEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 160, 300, 30));

        tfEmail2.setName("tfEmail2"); // NOI18N
        add(tfEmail2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 200, 300, 30));

        tfAddress1.setName("tfAddress1"); // NOI18N
        add(tfAddress1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 240, 300, 30));

        tfAddress2.setName("tfAddress2"); // NOI18N
        add(tfAddress2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 280, 300, 30));

        tfCity.setName("tfCity"); // NOI18N
        add(tfCity, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 320, 300, 30));

        tfProvince.setName("tfProvince"); // NOI18N
        add(tfProvince, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 360, 300, 30));

        tfPostcode.setName("tfPostcode"); // NOI18N
        add(tfPostcode, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 400, 300, 30));

        tfPhone.setName("tfPhone"); // NOI18N
        add(tfPhone, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 440, 300, 30));

        tfUserName.setName("tfUserName"); // NOI18N
        add(tfUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 480, 300, 30));

        lbPostcode.setForeground(resourceMap.getColor("lbPostcode.foreground")); // NOI18N
        lbPostcode.setText(resourceMap.getString("lbPostcode.text")); // NOI18N
        lbPostcode.setName("lbPostcode"); // NOI18N
        add(lbPostcode, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 400, -1, 30));

        lbProvinceError.setForeground(resourceMap.getColor("lbProvinceError.foreground")); // NOI18N
        lbProvinceError.setText(resourceMap.getString("lbProvinceError.text")); // NOI18N
        lbProvinceError.setName("lbProvinceError"); // NOI18N
        add(lbProvinceError, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 360, -1, 30));

        lbCityError.setForeground(resourceMap.getColor("lbCityError.foreground")); // NOI18N
        lbCityError.setText(resourceMap.getString("lbCityError.text")); // NOI18N
        lbCityError.setName("lbCityError"); // NOI18N
        add(lbCityError, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 320, -1, 30));

        lbAddress2Error.setForeground(resourceMap.getColor("lbAddress2Error.foreground")); // NOI18N
        lbAddress2Error.setText(resourceMap.getString("lbAddress2Error.text")); // NOI18N
        lbAddress2Error.setName("lbAddress2Error"); // NOI18N
        add(lbAddress2Error, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 280, -1, 30));

        lbfirstnameError.setForeground(resourceMap.getColor("lbfirstnameError.foreground")); // NOI18N
        lbfirstnameError.setText(resourceMap.getString("lbfirstnameError.text")); // NOI18N
        lbfirstnameError.setName("lbfirstnameError"); // NOI18N
        add(lbfirstnameError, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 80, -1, 30));

        jCheckBoxPremuim.setBackground(resourceMap.getColor("jCheckBoxPremuim.background")); // NOI18N
        jCheckBoxPremuim.setFont(resourceMap.getFont("jCheckBoxPremuim.font")); // NOI18N
        jCheckBoxPremuim.setText(resourceMap.getString("jCheckBoxPremuim.text")); // NOI18N
        jCheckBoxPremuim.setName("jCheckBoxPremuim"); // NOI18N
        add(jCheckBoxPremuim, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 600, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void jBAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAcceptActionPerformed
        // button "Accept and Continue":
        RegisterController controller = new RegisterController();

        Customer customer = new Customer();
        User user = new User();
        PremiumCustomer premiumcustomer = new PremiumCustomer();

        String username = this.tfUserName.getText();
        String password = StringHelper.CharArrayToString(this.pfPassword.getPassword());
        String password2 = StringHelper.CharArrayToString(this.pfPassword2.getPassword());
        String firstname = this.tfFirstName.getText();
        String lastname = this.tfLastName.getText();
        String email = this.tfEmail.getText();
        String email2 = this.tfEmail2.getText();
        String address1 = this.tfAddress1.getText();
        String address2 = this.tfAddress2.getText();
        String city = this.tfCity.getText();
        String province = this.tfProvince.getText();
        String postcode = this.tfPostcode.getText();
        String phone = this.tfPhone.getText();

        boolean validLabel = true;
        boolean validphone = true;



        if (password.equals(""))
        {
            this.lbPasswordError.setVisible(true);
            validLabel = false;
        }
        else if ((password.length() < 5) || (password.length() > 12))
        {
            this.lbPasswordError.setVisible(true);
            validLabel = false;
        }
        else if ((!(password.equals(password2))) && (password.length() >= 5) && (password.length() <= 12))
        {
            this.lbPasswordError1.setVisible(true);
            this.lbPasswordError.setVisible(false);
            validLabel = false;
        }
        else
        {
            this.lbPasswordError.setVisible(false);
            this.lbPasswordError1.setVisible(false);
            user.setPassword(Encryption.md5(password));
        }


        if (!Validator.validateStringFormat(username))
        {
            this.lbUserName.setText("The format is invalid");
            this.lbUserName.setVisible(true);
            validLabel = false;
        }
        else if (username.equals(""))
        {
            this.lbUserName.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbUserName.setVisible(false);
            user.setUsername(username);
            user.setUserRoleID(UserRoles.Customer.ordinal());
        }



        if (!Validator.validateNameFormat(firstname))
        {
            this.lbfirstnameError.setText("The information is invalid");
            this.lbfirstnameError.setVisible(true);
            validLabel = false;
        }
        else if (firstname.equals(""))
        {
            this.lbfirstnameError.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbfirstnameError.setVisible(false);
            customer.setFirstName(firstname);
        }

        if (!Validator.validateNameFormat(lastname))
        {
            this.lblastnameError.setText("The information is invalid");
            this.lblastnameError.setVisible(true);
            validLabel = false;
        }
        else if (lastname.equals(""))
        {
            this.lblastnameError.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lblastnameError.setVisible(false);
            customer.setLastName(lastname);
        }

        if (email.equals(""))
        {
            this.lbEmailError.setVisible(true);
            validLabel = false;
        }
        else if ((!Validator.validateEmailFormat(email)) || (!email.contains("@")) || (!email.contains(".")))
        {
            this.lbEmailError.setText("The information is invalid");
            this.lbEmailError.setVisible(true);
            validLabel = false;
        }
        else if ((email.contains("@")) && (email.contains(".")) && (!(email.equals(email2))))
        {
            this.lbEmailError.setVisible(false);
            this.lbEmail2Error.setVisible(true);
            validLabel = false;
        }
        else if ((email.contains("@")) && (email.contains(".")) && (email.equals(email2)))
        {
            this.lbEmailError.setVisible(false);
            this.lbEmail2Error.setVisible(false);
            customer.setEmailAddress(email);

        }

        if (!Validator.validateAddressFormat(address2))
        {
            this.lbAddress1Error.setText("The information is invalid");
            this.lbAddress1Error.setVisible(true);
            validLabel = false;
        }
        else if (address1.equals(""))
        {
            this.lbAddress1Error.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbAddress1Error.setVisible(false);
            customer.setAddressLine1(address1);
        }

        if (!Validator.validateNameFormat(city))
        {
            this.lbCityError.setText("The information is invalid");
            this.lbCityError.setVisible(true);
            validLabel = false;
        }
        else if (city.equals(""))
        {
            this.lbCityError.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbCityError.setVisible(false);
            customer.setCity(city);
        }


        if (!Validator.validateNameFormat(province))
        {
            this.lbProvinceError.setText("The information is invalid");
            this.lbProvinceError.setVisible(true);
            validLabel = false;
        }
        else if (province.equals(""))
        {
            this.lbProvinceError.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbProvinceError.setVisible(false);
            customer.setProvince(province);
        }

        if (!Validator.validatePostcodeFormat(postcode))
        {
            this.lbPostcode.setText("The information is invalid");
            this.lbPostcode.setVisible(true);
            validLabel = false;
        }
        else if (postcode.equals(""))
        {
            this.lbPostcode.setVisible(true);
            validLabel = false;
        }
        else if (postcode.length() != 6)
        {
            this.lbPostcode.setText("6 characters");
            this.lbPostcode.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbPostcode.setVisible(false);
            customer.setPostcode(postcode);
        }

        if (!Validator.validatePhoneNumberFormat(phone))
        {
            this.lbPhone.setText("The information is invalid");
            this.lbPhone.setVisible(true);
            validLabel = false;
        }
        else if (phone.equals(""))
        {
            this.lbPhone.setVisible(false);
            customer.setPhoneNo(phone);
        }
        else if (phone.length() != 10)
        {
            this.lbPhone.setText("10 numbers");
            this.lbPhone.setVisible(true);
            validLabel = false;
        }
        else
        {
            for (int i = 0; i < 10; ++i)
            {
                if (!Character.isDigit(phone.charAt(i)))
                {
                    validphone = false;
                    validLabel = false;
                }
            }
            if (validphone)
            {
                this.lbPhone.setVisible(false);
                customer.setPhoneNo(phone);
            }
        }


        if (!Validator.validateAddressFormat(address2))
        {
            this.lbAddress2Error.setVisible(true);
            validLabel = false;
        }
        else
        {
            this.lbAddress2Error.setVisible(false);
            customer.setAddressLine2(address2);
        }

        Collection<User> validatedUser = this.getUserDAO().getUsersDO(user);

        if (validatedUser.size() != 0)
        {
            this.lbUserName.setText("User exists");
            this.lbUserName.setVisible(true);
            validLabel = false;
        }
        else if ((validatedUser.size() == 0) && (validLabel))
        {
            int i = userDAO.insertUser(user);
            customer.setCustomerID(i);
            premiumcustomer.setCustomerID(i);
            try
            {
                controller.registerCustomer(customer);
                controller.RegisterUser(user);
            }
            catch (CertificateException ex)
            {
                Logger.getLogger(CustomerRegisterPanel.class.getName()).log(Level.SEVERE, null, ex);
            }

        }


        if ((this.jCheckBoxPremuim.isSelected()) && (validLabel))
        {
            //the credit card page

            Order order = new Order();
            order.setCustomerID(customer.getCustomerID());
            order.setOrderDate(DateTimeHelper.getCurrentDateTime());
            order.setOrderType(OrderType.YearlyFee.ordinal());
            order.setCost(YEARLYFEE + TaxManager.calculateTax(YEARLYFEE));
            order.setShippingMethodID(2);
            Event yearlyFeeEvent = new Event();
            yearlyFeeEvent.setName("Yearly Fee");
            yearlyFeeEvent.setCancelled(true);

            DAOFactory.getInstance().getEventDAO().getEventsDO(yearlyFeeEvent);
            order.setEventID(yearlyFeeEvent.getEventID());

            OrderController orderController = new OrderController();
            order.setOrderID(orderController.createOrder(order));
            if (order.getOrderID() > 0)
            {
                ticketmaster.businessobject.Customer userBO = new ticketmaster.businessobject.Customer();
                userBO.setUserID(customer.getCustomerID());
                userBO.setPassword(user.getPassword());
                userBO.setRole(UserRoles.Customer);
                userBO.setUsername(user.getUsername());
                LoginUser.setUser(userBO);
                PurchaseType.setPurchaseType(PurchaseTypes.New);
                PaymentReviewView prv = new PaymentReviewView(YEARLYFEE, TaxManager.calculateTax(YEARLYFEE));
                prv.setOrder(order);
                if (this.getMainFrame() != null)
                {
                    prv.setMainFrame(this.getMainFrame());
                }
                prv.setBookedTickets(this.getBookedTickets());
                prv.setPnlParent(this.getPnlParent());
                this.getPnlParent().removeAll();
                this.getPnlParent().add(prv);
                this.getPnlParent().validate();
                this.getPnlParent().repaint();
            }

//            premiumcustomer.setRegistrationDate(DateTimeHelper.getCurrentDateTime());
//            premiumcustomer.setPoints(30);
//            controller.RegisterPremiumCustomer(premiumcustomer);

        }
        else if ((!this.jCheckBoxPremuim.isSelected()) && (validLabel))
        {
            HomeView hv = new HomeView();
            this.getMainFrame().setVisible(false);
            hv.setVisible(true);
        }
        // sendEmailToNewCustomer by xiaolong xue

        List<String> emailaddr = new ArrayList<String>();
        emailaddr.add(email);
        Email myemail = new Email();
        myemail.setEmailAddress(emailaddr);
        myemail.setSubject("Registration Success Notice");
        myemail.setMessage("Dear " + lastname + ",<br><br> You have successfully registered in Ticket Master system.");
        try
        {
            EmailSender emailSender = new EmailSender();
            emailSender.sendSSLMessage(myemail);
        }
        catch (AddressException ex)
        {
            Logger.getLogger(AdministratorHomeView.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (MessagingException ex)
        {
            Logger.getLogger(AdministratorHomeView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBAcceptActionPerformed

    private void jBBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBackActionPerformed
        // button "Back":
        HomeView homeView = new HomeView();
        homeView.setVisible(true);
        this.getMainFrame().setVisible(false);
    }//GEN-LAST:event_jBBackActionPerformed

    private void jBLogInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBLogInActionPerformed
        // button "Log In":
        HomeView homeView = new HomeView();
        homeView.setVisible(true);
        this.getMainFrame().setVisible(false);
    }//GEN-LAST:event_jBLogInActionPerformed

    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                JFrame frame = new JFrame();
                CustomerRegisterPanel crp = new CustomerRegisterPanel();
                frame.setSize(1000, 800);
                frame.add(crp);
                frame.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAccept;
    private javax.swing.JButton jBBack;
    private javax.swing.JButton jBLogIn;
    private javax.swing.JCheckBox jCheckBoxPremuim;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lbAddress1Error;
    private javax.swing.JLabel lbAddress2Error;
    private javax.swing.JLabel lbCityError;
    private javax.swing.JLabel lbEmail2Error;
    private javax.swing.JLabel lbEmailError;
    private javax.swing.JLabel lbPasswordError;
    private javax.swing.JLabel lbPasswordError1;
    private javax.swing.JLabel lbPhone;
    private javax.swing.JLabel lbPostcode;
    private javax.swing.JLabel lbProvinceError;
    private javax.swing.JLabel lbUserName;
    private javax.swing.JLabel lbUsername;
    private javax.swing.JLabel lbUsername1;
    private javax.swing.JLabel lbUsername10;
    private javax.swing.JLabel lbUsername11;
    private javax.swing.JLabel lbUsername12;
    private javax.swing.JLabel lbUsername2;
    private javax.swing.JLabel lbUsername3;
    private javax.swing.JLabel lbUsername4;
    private javax.swing.JLabel lbUsername5;
    private javax.swing.JLabel lbUsername6;
    private javax.swing.JLabel lbUsername7;
    private javax.swing.JLabel lbUsername8;
    private javax.swing.JLabel lbUsername9;
    private javax.swing.JLabel lbfirstnameError;
    private javax.swing.JLabel lblastnameError;
    private javax.swing.JPasswordField pfPassword;
    private javax.swing.JPasswordField pfPassword2;
    private javax.swing.JTextField tfAddress1;
    private javax.swing.JTextField tfAddress2;
    private javax.swing.JTextField tfCity;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfEmail2;
    private javax.swing.JTextField tfFirstName;
    private javax.swing.JTextField tfLastName;
    private javax.swing.JTextField tfPhone;
    private javax.swing.JTextField tfPostcode;
    private javax.swing.JTextField tfProvince;
    private javax.swing.JTextField tfUserName;
    // End of variables declaration//GEN-END:variables

    private void initPasswordFields()
    {
        this.pfPassword.setText("");
        this.pfPassword2.setText("");
    }

    public UserDAO getUserDAO()
    {
        if (userDAO == null)
        {
            userDAO = DAOFactory.getInstance().getUserDAO();
        }
        return userDAO;
    }
}
